﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using ContactManager.Models.Validation;
using System;
using System.Linq;
using System.Text;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Configuration.DSL;
using System.Web.Routing;
using ContactManager.Models.Entities;
using ContactManager.Models.Repositories;

namespace ContactManager.Models
{

    public class StructureMapControllerFactory : DefaultControllerFactory
    {

        protected override IController GetControllerInstance(RequestContext requestContext,Type controllerType)
        {

            if (controllerType == null) return null;

            Controller c;

            try
            {
                c = ObjectFactory.GetInstance(controllerType) as Controller;
                if (c != null) return c;

                return c;

            }

            catch (StructureMapException)
            {

                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());

                throw;

            }

        }

    }
    public class StructureMapConfiguration
    {

        public static void Configure()
        {

            ObjectFactory.Initialize(InitializeStructureMap);

        }



        private static void InitializeStructureMap(IInitializationExpression x)
        {
            
            //Registry.BuildWithExpression<ForRequestedType<IContactManagerService>().TheDefault.Is.OfConcreteType<ContactManagerService>(); 
            
            ObjectFactory.Initialize(registry =>
            {

                //registry.For<IContactManagerService>().TheDefaultIsConcreteType<ContactManagerService>(); 

                registry.For<IContactManagerService>().Use<ContactManagerService>();
                //registry.For<IContactManagerRepository>().Use<ContactManagerRepositoryAr>();
                

                
            });
           
           // x.Scan(y =>
            //{

              //  y.Assembly("ContactManager");

               // y.Assembly("MvcWithNHibernate.Services");

                //y.With<DefaultConventionScanner>();

            //}

                //);

        }

    } 

}